package controller

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-bigdata-service/infrastructure/common"
	"git.mycaigou.com/gfyx/micro-gfyx-bigdata-service/infrastructure/common/errcode"
	"github.com/gin-gonic/gin"
	"net/http"
)

var (
//tokenNotExist = errors.New("token not exist")
)

type BaseController struct{}

//
// 获取token
//
//func (c *BaseController) GetToken(ctx *gin.Context) (string, error) {
//	authTokenContext, ok := ctx.Get(constants.CONTEXT_AUTH_TOKEN_KEY)
//	if !ok {
//		return "", tokenNotExist
//	}
//
//	authTokenStr, ok := authTokenContext.(string)
//	if !ok {
//		return "", tokenNotExist
//	}
//	return authTokenStr, nil
//}

// Success
// 成果返回数据
func (c *BaseController) Success(ctx *gin.Context, data interface{}) {
	result := gin.H{
		"success":    true,
		"message":    "操作成功",
		"error_code": 0,
		"data":       data,
	}

	ctx.JSON(http.StatusOK, result)
}

//Fail
//失败返回数据数据
func (c *BaseController) Fail(ctx *gin.Context, code int, msg string) {
	result := map[string]interface{}{
		"success":    false,
		"message":    common.WrapError(msg),
		"error_code": code,
		"data":       nil,
	}

	ctx.JSON(http.StatusOK, result)
}

func (c *BaseController) GetErrCode(err error) int {
	grpcErr := rpc_client.GetGrpcError(err)
	errCode := errcode.COMMON_ERROR
	if grpcErr.GetCode() != 0 {
		errCode = grpcErr.GetCode()
	}
	return errCode
}
